package com.tepth.remote.core.utils.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Description:创造单线程池
 *
 * @author Hequn.Lee
 * @date 2018/11/12
 */
public class ControlSingleThread {

    /**
     * 回调接口
     */
    private RunnableCallback mCallback;
    /**
     * h回调
     */
    private final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (mCallback != null) {
                mCallback.callback();
            }
        }
    };

    /**
     * 单例模式
     */
    private static final class Holder {
        private static final ExecutorService SINGLE_THREAD_POOL = new ThreadPoolExecutor(5, 5,
                60L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), new ControlThreadFactory());
        private static final ControlSingleThread INSTANCE = new ControlSingleThread();
    }

    /**
     * 获得实例
     *
     * @return 实例
     */
    public static ControlSingleThread getInstance() {
        return Holder.INSTANCE;
    }

    /**
     * 获取单线程池
     *
     * @return 单线程池
     */
    public static ExecutorService getThreadPool() {
        return Holder.SINGLE_THREAD_POOL;
    }

    /**
     * 设置回调
     *
     * @param callback 线程回调
     * @return 单线程池
     */
    public ControlSingleThread setCallback(RunnableCallback callback) {
        mCallback = callback;
        return this;
    }

    /**
     * 获取回调
     *
     * @return 回调
     */
    public Runnable getRunnable() {
        return mRunnable;
    }
}
